#include "mainclass.h"
#include "ui_mainclass.h"
#include <QtCore/QCoreApplication>
#include <QPropertyAnimation>

#include <QTimer>

MainClass::MainClass(QWidget *parent) :
    QMainWindow(parent), ui(new Ui::MainClass)
{
    ui->setupUi(this);
    this->showFullScreen();

    battleEngine = new BattleEngine(ui);

    //New Game Button Effects
    connect(ui->newGameButton,SIGNAL(pressed()),this,SLOT(lightUpButton()));
    connect(ui->newGameButton,SIGNAL(released()),this,SLOT(changeScreen()));
    //Exit Game Button Effects
    connect(ui->exitGameButton_2,SIGNAL(pressed()),this,SLOT(lightUpButton()));
    connect(ui->exitGameButton_2,SIGNAL(released()),this,SLOT(changeScreen()));

    //Inside titleScreen
        connect(ui->splash,SIGNAL(clicked()),this,SLOT(changeScreen()));
    //Inside newGame Screen
        //Faction Types
        connect(ui->military,SIGNAL(pressed()),this,SLOT(lightUpNGSButton()));
        connect(ui->economy,SIGNAL(pressed()),this,SLOT(lightUpNGSButton()));
        connect(ui->social,SIGNAL(pressed()),this,SLOT(lightUpNGSButton()));
        connect(ui->technology,SIGNAL(pressed()),this,SLOT(lightUpNGSButton()));
        //Back Button
        connect(ui->backnewG,SIGNAL(pressed()),this,SLOT(lightUpButton()));
        connect(ui->backnewG,SIGNAL(released()),this,SLOT(changeScreen()));
        //Next Button
        connect(ui->nextnewG,SIGNAL(pressed()),this,SLOT(lightUpButton()));
        connect(ui->nextnewG,SIGNAL(released()),this,SLOT(changeScreen()));
    //Inside chooseEnemy Screen
        //Enemy Difficulty
        connect(ui->easy,SIGNAL(pressed()),this,SLOT(lightUpCESButton()));
        connect(ui->medium,SIGNAL(pressed()),this,SLOT(lightUpCESButton()));
        connect(ui->hard,SIGNAL(pressed()),this,SLOT(lightUpCESButton()));
        connect(ui->extreme,SIGNAL(pressed()),this,SLOT(lightUpCESButton()));
        //Back Button
        connect(ui->backCH,SIGNAL(pressed()),this,SLOT(lightUpButton()));
        connect(ui->backCH,SIGNAL(released()),this,SLOT(changeScreen()));
        //Next Button
        connect(ui->nextCH,SIGNAL(pressed()),this,SLOT(lightUpButton()));
        connect(ui->nextCH,SIGNAL(released()),this,SLOT(changeScreen()));

    ui->gameDisplay->setCurrentIndex(1);

    connect(ui->cmd1,SIGNAL(clicked()),this,SLOT(changeScreen()));
    connect(ui->cmd2,SIGNAL(clicked()),this,SLOT(changeScreen()));
    connect(ui->cmd3,SIGNAL(clicked()),this,SLOT(changeScreen()));

//    QPropertyAnimation* animation = new QPropertyAnimation(ui->splash, "opacity");
//    animation->setDuration(1000);
//    animation->setStartValue(100);
//    animation->setEndValue(0);

//    animation->start();

    QTimer* spawn = new QTimer();
    connect(spawn,SIGNAL(timeout()),battleEngine,SLOT(spawnInfLv1()));

    battleEngine->startCombatSession();

    battleEngine->spawnBuilding("CommandCenter",1,true);
    spawn->start(1000);

}

MainClass::~MainClass()
{
    delete ui;
}

void MainClass::lightUpCESButton() {
    //Light up Choose Enemy Screen Buttons
    QIcon icon1, icon2, icon3, icon4;
    QPushButton* btn = (QPushButton*)QObject::sender();

    if (btn == ui->easy) {
        icon1.addFile(QString::fromUtf8(":/Images/buttons/enemy/buttons/enemy/easyOn.png"), QSize(), QIcon::Normal, QIcon::Off);
        icon2.addFile(QString::fromUtf8(":/Images/buttons/enemy/buttons/enemy/medium.png"), QSize(), QIcon::Normal, QIcon::Off);
        icon3.addFile(QString::fromUtf8(":/Images/buttons/enemy/buttons/enemy/hard.png"), QSize(), QIcon::Normal, QIcon::Off);
        icon4.addFile(QString::fromUtf8(":/Images/buttons/enemy/buttons/enemy/superhard.png"), QSize(), QIcon::Normal, QIcon::Off);
        ui->easy->setIcon(icon1);
        ui->medium->setIcon(icon2);
        ui->hard->setIcon(icon3);
        ui->extreme->setIcon(icon4);
        ui->diffDesc->setPixmap(QPixmap(QString::fromUtf8(":/Images/text/newGame/text/chooseEnemy/easyText.png")));
        ui->diffDescText->setText("Difficulty: Easy\n\n> A ravaged nation will have a hard\n    time fighting your forces due to lack\n    of resources and manpower.\n\n> Recommended for beginners who\n    wants to learn the game.");
    }
    else if (btn == ui->medium) {
        icon1.addFile(QString::fromUtf8(":/Images/buttons/enemy/buttons/enemy/easy.png"), QSize(), QIcon::Normal, QIcon::Off);
        icon2.addFile(QString::fromUtf8(":/Images/buttons/enemy/buttons/enemy/mediumOn.png"), QSize(), QIcon::Normal, QIcon::Off);
        icon3.addFile(QString::fromUtf8(":/Images/buttons/enemy/buttons/enemy/hard.png"), QSize(), QIcon::Normal, QIcon::Off);
        icon4.addFile(QString::fromUtf8(":/Images/buttons/enemy/buttons/enemy/superhard.png"), QSize(), QIcon::Normal, QIcon::Off);
        ui->easy->setIcon(icon1);
        ui->medium->setIcon(icon2);
        ui->hard->setIcon(icon3);
        ui->extreme->setIcon(icon4);
        ui->diffDesc->setPixmap(QPixmap(QString::fromUtf8(":/Images/text/newGame/text/chooseEnemy/mediumText.png")));
        ui->diffDescText->setText("Difficulty: Medium\n\n> A developing nation will have some\n    resources in stock and will be able to\n    fight your foces to some extent.\n\n> Recommended for players who\n    are looking for a quick match.");
    }
    else if (btn == ui->hard) {
        icon1.addFile(QString::fromUtf8(":/Images/buttons/enemy/buttons/enemy/easy.png"), QSize(), QIcon::Normal, QIcon::Off);
        icon2.addFile(QString::fromUtf8(":/Images/buttons/enemy/buttons/enemy/medium.png"), QSize(), QIcon::Normal, QIcon::Off);
        icon3.addFile(QString::fromUtf8(":/Images/buttons/enemy/buttons/enemy/hardOn.png"), QSize(), QIcon::Normal, QIcon::Off);
        icon4.addFile(QString::fromUtf8(":/Images/buttons/enemy/buttons/enemy/superhard.png"), QSize(), QIcon::Normal, QIcon::Off);
        ui->easy->setIcon(icon1);
        ui->medium->setIcon(icon2);
        ui->hard->setIcon(icon3);
        ui->extreme->setIcon(icon4);
        ui->diffDesc->setPixmap(QPixmap(QString::fromUtf8(":/Images/text/newGame/text/chooseEnemy/hardText.png")));
        ui->diffDescText->setText("Difficulty: Hard\n\n> A developed nation will have a lot\n    of resources in stock and more\n    experienced commanders.\n\n> Recommended for players who\n    are looking for a challenge.");
    }
    else if (btn == ui->extreme) {
        icon1.addFile(QString::fromUtf8(":/Images/buttons/enemy/buttons/enemy/easy.png"), QSize(), QIcon::Normal, QIcon::Off);
        icon2.addFile(QString::fromUtf8(":/Images/buttons/enemy/buttons/enemy/medium.png"), QSize(), QIcon::Normal, QIcon::Off);
        icon3.addFile(QString::fromUtf8(":/Images/buttons/enemy/buttons/enemy/hard.png"), QSize(), QIcon::Normal, QIcon::Off);
        icon4.addFile(QString::fromUtf8(":/Images/buttons/enemy/buttons/enemy/superhardOn.png"), QSize(), QIcon::Normal, QIcon::Off);
        ui->easy->setIcon(icon1);
        ui->medium->setIcon(icon2);
        ui->hard->setIcon(icon3);
        ui->extreme->setIcon(icon4);
        ui->diffDesc->setPixmap(QPixmap(QString::fromUtf8(":/Images/text/newGame/text/chooseEnemy/extremeText.png")));
        ui->diffDescText->setText("Difficulty: Extreme\n\n>  A world power has excessive\n    amounts of armaments, resources\n    and experienced commanders.\n\n>  Not recommended for anyone!\n    Facing a world power is suicide!");
    }
}

void MainClass::lightUpNGSButton() {
    //Light up New Game Screen Buttons
    QIcon icon1, icon2, icon3, icon4;
    QPushButton* btn = (QPushButton*)QObject::sender();

    if (btn == ui->military) {
        icon1.addFile(QString::fromUtf8(":/Images/buttons/newGame/buttons/newGame/militaryOn.png"), QSize(), QIcon::Normal, QIcon::Off);
        icon2.addFile(QString::fromUtf8(":/Images/buttons/newGame/buttons/newGame/economy.png"), QSize(), QIcon::Normal, QIcon::Off);
        icon3.addFile(QString::fromUtf8(":/Images/buttons/newGame/buttons/newGame/social.png"), QSize(), QIcon::Normal, QIcon::Off);
        icon4.addFile(QString::fromUtf8(":/Images/buttons/newGame/buttons/newGame/technology.png"), QSize(), QIcon::Normal, QIcon::Off);
        ui->military->setIcon(icon1);
        ui->economy->setIcon(icon2);
        ui->social->setIcon(icon3);
        ui->technology->setIcon(icon4);
        ui->factionDesc->setPixmap(QPixmap(QString::fromUtf8(":/Images/text/newGame/text/newGame/militaryText.png")));
        ui->factionDescText->setText("Special Skill:\n    - Blitzkreig\n        > Increase the attack power  of your\n            troops for 30 seconds.\n        > Decrease the cost of units for 30\n            seconds.\nTraits:\n    - Troops have stronger:\n        > Attack Power\n        > Defense Power\n\n* This type of faction is recommended for\nleaders who wants to rule with an iron fist!");
    }
    else if (btn == ui->economy) {
        icon1.addFile(QString::fromUtf8(":/Images/buttons/newGame/buttons/newGame/military.png"), QSize(), QIcon::Normal, QIcon::Off);
        icon2.addFile(QString::fromUtf8(":/Images/buttons/newGame/buttons/newGame/economyOn.png"), QSize(), QIcon::Normal, QIcon::Off);
        icon3.addFile(QString::fromUtf8(":/Images/buttons/newGame/buttons/newGame/social.png"), QSize(), QIcon::Normal, QIcon::Off);
        icon4.addFile(QString::fromUtf8(":/Images/buttons/newGame/buttons/newGame/technology.png"), QSize(), QIcon::Normal, QIcon::Off);
        ui->military->setIcon(icon1);
        ui->economy->setIcon(icon2);
        ui->social->setIcon(icon3);
        ui->technology->setIcon(icon4);
        ui->factionDesc->setPixmap(QPixmap(QString::fromUtf8(":/Images/text/newGame/text/newGame/economyText.png")));
        ui->factionDescText->setText("Special Skill:\n    - Economic Boom\n        > Temporarily increases the output\n            of your economic centers.\nTraits:\n    - Economic Centers produce more\n      earnings than usual.\n    - Kills produce more money than usual.\n\n* This type of faction is recommended for\nleaders who choose to lead more carefully!");
    }
    else if (btn == ui->social) {
        icon1.addFile(QString::fromUtf8(":/Images/buttons/newGame/buttons/newGame/military.png"), QSize(), QIcon::Normal, QIcon::Off);
        icon2.addFile(QString::fromUtf8(":/Images/buttons/newGame/buttons/newGame/economy.png"), QSize(), QIcon::Normal, QIcon::Off);
        icon3.addFile(QString::fromUtf8(":/Images/buttons/newGame/buttons/newGame/socialOn.png"), QSize(), QIcon::Normal, QIcon::Off);
        icon4.addFile(QString::fromUtf8(":/Images/buttons/newGame/buttons/newGame/technology.png"), QSize(), QIcon::Normal, QIcon::Off);
        ui->military->setIcon(icon1);
        ui->economy->setIcon(icon2);
        ui->social->setIcon(icon3);
        ui->technology->setIcon(icon4);
        ui->factionDesc->setPixmap(QPixmap(QString::fromUtf8(":/Images/text/newGame/text/newGame/socialText.png")));
        ui->factionDescText->setText("Special Skill:\n    - Worker's Revolution\n        > Creates a mass of infantry units.\nTraits:\n    - Buildings Cost Less\n    - Units Cost Less\n    - Upgrades Cost Less\n\n* This type of faction is recommended\nfor leaders who choose to be fair and\njust in their rule!");
    }
    else if (btn == ui->technology) {
        icon1.addFile(QString::fromUtf8(":/Images/buttons/newGame/buttons/newGame/military.png"), QSize(), QIcon::Normal, QIcon::Off);
        icon2.addFile(QString::fromUtf8(":/Images/buttons/newGame/buttons/newGame/economy.png"), QSize(), QIcon::Normal, QIcon::Off);
        icon3.addFile(QString::fromUtf8(":/Images/buttons/newGame/buttons/newGame/social.png"), QSize(), QIcon::Normal, QIcon::Off);
        icon4.addFile(QString::fromUtf8(":/Images/buttons/newGame/buttons/newGame/technologyOn.png"), QSize(), QIcon::Normal, QIcon::Off);
        ui->military->setIcon(icon1);
        ui->economy->setIcon(icon2);
        ui->social->setIcon(icon3);
        ui->technology->setIcon(icon4);
        ui->factionDesc->setPixmap(QPixmap(QString::fromUtf8(":/Images/text/newGame/text/newGame/techText.png")));
        ui->factionDescText->setText("Special Skill:\n    - Ion Cannon\n        > Destroys ALL units in the field.\nTraits:\n    - Technology Centers produce more\n      technology points.\n    - Everything is generally more\n      expensive.\n\n* This type of faction is recommended\nfor leaders who wants to unlock the full\npotential of his or her faction.");
    }


}

void MainClass::lightUpButton() {
    QIcon icon1;
    QPushButton* btn = (QPushButton*)QObject::sender();
    bool hasIcon = true;

    if (btn == ui->newGameButton) {
        icon1.addFile(QString::fromUtf8(":/Images/buttons/mainMenu/buttons/mainMenu/newGameOn.png"), QSize(), QIcon::Normal, QIcon::Off);
    }
    else if (btn == ui->backnewG || btn == ui->backCH) {
        icon1.addFile(QString::fromUtf8(":/Images/buttons/newGame/buttons/backButtonOn.png"), QSize(), QIcon::Normal, QIcon::Off);
    }
    else if (btn == ui->exitGameButton_2) {
        icon1.addFile(QString::fromUtf8(":/Images/buttons/mainMenu/buttons/mainMenu/exitOn.png"), QSize(), QIcon::Normal, QIcon::Off);
    }
    else if (btn == ui->nextnewG || btn == ui->nextCH) {
        icon1.addFile(QString::fromUtf8(":/Images/buttons/newGame/buttons/nextButtonOn.png"), QSize(), QIcon::Normal, QIcon::Off);
    }
    else
        hasIcon = false;

    if(hasIcon)
        btn->setIcon(icon1);
}

void MainClass::lightDownButton(QPushButton* btn) {
    QIcon icon1;
    bool hasIcon = true;

    if (btn == ui->newGameButton) {
        icon1.addFile(QString::fromUtf8(":/Images/buttons/mainMenu/buttons/mainMenu/newGame.png"), QSize(), QIcon::Normal, QIcon::Off);
    }
    else if (btn == ui->backnewG || btn == ui->backCH) {
        icon1.addFile(QString::fromUtf8(":/Images/buttons/newGame/buttons/backButton.png"), QSize(), QIcon::Normal, QIcon::Off);
    }
    else if (btn == ui->exitGameButton_2) {
        icon1.addFile(QString::fromUtf8(":/Images/buttons/mainMenu/buttons/mainMenu/exit.png"), QSize(), QIcon::Normal, QIcon::Off);
    }
    else if (btn == ui->nextnewG || btn == ui->nextCH) {
        icon1.addFile(QString::fromUtf8(":/Images/buttons/newGame/buttons/nextButton.png"), QSize(), QIcon::Normal, QIcon::Off);
    }
    else
        hasIcon = false;

    if(hasIcon)
        btn->setIcon(icon1);
}

void MainClass::changeScreen() {
    QPushButton* btn = (QPushButton *)(QObject::sender());
    int index = 0;

    if (btn == ui->newGameButton)
        index = 2;
    else if (btn == ui->backnewG)
        index = 1;
    else if (btn == ui->nextnewG)
        index = 3;
    else if (btn == ui->backCH)
        index = 2;
    else if (btn == ui->nextCH)
        index = 4;
    else if (btn == ui->splash)
        index = 1;
    else if (btn == ui->cmd1)
        index = 0;
    else if (btn == ui->cmd2)
        index = 1;
    else if (btn == ui->cmd3)
        index = 2;

    lightDownButton(btn);

    if(btn != ui->cmd1 && btn != ui->cmd2 && btn != ui->cmd3)
        ui->screens->setCurrentIndex(index);
    else
        ui->gameDisplay->setCurrentIndex(index);

}

BattleEngine* MainClass::getBattleEngine() {
    return battleEngine;
}

void MainClass::showExpanded() {

#ifdef  Q_OS_SYMBIAN
    showFullScreen();
#elif   defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
    showMaximized();
#else
    show();
#endif
}
